package com.qianfeng.xiaxk.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.HttpHandler;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.qianfeng.contact.CharacterParser;
import com.qianfeng.contact.SideBar;
import com.qianfeng.contact.SortAdapter;
import com.qianfeng.xiaxk.R;
import com.qianfeng.xiaxk.bean.ContactBean;
import com.qianfeng.xiaxk.bean.ContactDetail;
import com.qianfeng.xiaxk.utils.LoginUtils;
import com.qianfeng.xiaxk.utils.NetStateUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@ContentView(R.layout.activity_chat_address_book)
public class ChatAddressBookActivity extends AppCompatActivity implements SideBar.OnTouchingLetterChangedListener {

    HttpUtils httpUtils;
    HttpHandler handler;
    @ViewInject(R.id.chat_listview)
    ListView contactListView;
    @ViewInject(R.id.chat_sidebar)
    SideBar sideBar;
    @ViewInject(R.id.chat_letterindicator_tv)
    TextView indecatorTextView;
    List<ContactBean> friendList = new ArrayList();
    SortAdapter adapter ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ViewUtils.inject(this);
        httpUtils = new HttpUtils();
        initView();
        initData();
    }

    private void initView() {
        adapter = new SortAdapter(this,friendList);
        contactListView.setAdapter(adapter);
        sideBar.setTextView(indecatorTextView);
        sideBar.setOnTouchingLetterChangedListener(this);
    }

    private void initData() {
        //http://api2.xiaxk.com/api/Friend?userid=12642&sex=0
        StringBuffer url = new StringBuffer("http://api2.xiaxk.com/api/Friend?");
        HashMap<String,String>map = LoginUtils.getLoginInfo(this);
        url.append("userid").append("=").append(map.get("vid")).append("&");
        url.append("sex=0");
        Log.e("",url.toString());
        if(NetStateUtils.isConnected(this)) {
            httpUtils.send(HttpRequest.HttpMethod.GET, url.toString(), new RequestCallBack<String>() {
                @Override
                public void onSuccess(ResponseInfo<String> responseInfo) {
                    JSONObject object = JSON.parseObject(responseInfo.result);
                    List<ContactDetail>list = JSON.parseArray(object.getString("friendList"), ContactDetail.class);
                    ArrayList<ContactBean>beanList = new ArrayList<>();
                    ContactBean bean;
                    for(int i=0;i<list.size();i++){
                        bean = new ContactBean();
                        bean.setDetail(list.get(i));
                        bean.setStartChar(CharacterParser.getInstance().getSelling(list.get(i).getNickname()).substring(0, 1));
                        beanList.add(bean);
                    }
                    friendList.addAll(beanList);
                    adapter.notifyDataSetChanged();
                }

                @Override
                public void onFailure(HttpException e, String s) {
                    Log.e("","数据加载失败："+s);
                }
            });
        }else{
            Toast.makeText(this,"网络连接异常",Toast.LENGTH_SHORT).show();
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.cancel();
    }

    @Override
    public void onTouchingLetterChanged(String s) {
        // 该字母首次出现的位置
        int position = adapter.getPositionForSection(s.charAt(0));
        if (position != -1) {
            contactListView.setSelection(position);
        }
    }
}
